package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpSellerStoreEuResponsiblePerson;
import com.erp.gather.domain.bo.ErpSellerStoreEuResponsiblePersonBo;
import com.erp.gather.domain.vo.ErpSellerStoreEuResponsiblePersonVo;
import com.erp.gather.mapper.ErpSellerStoreEuResponsiblePersonMapper;
import com.erp.gather.service.IErpSellerStoreEuResponsiblePersonService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 店铺欧盟负责人Service业务层处理
 *
 * @author erp
 * @date 2023-05-08
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreEuResponsiblePersonServiceImpl implements IErpSellerStoreEuResponsiblePersonService {

    private final ErpSellerStoreEuResponsiblePersonMapper baseMapper;

    /**
     * 查询店铺欧盟负责人
     */
    @Override
    public ErpSellerStoreEuResponsiblePersonVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺欧盟负责人列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreEuResponsiblePersonVo> queryPageList(ErpSellerStoreEuResponsiblePersonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreEuResponsiblePerson> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreEuResponsiblePersonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺欧盟负责人列表
     */
    @Override
    public List<ErpSellerStoreEuResponsiblePersonVo> queryList(ErpSellerStoreEuResponsiblePersonBo bo) {
        LambdaQueryWrapper<ErpSellerStoreEuResponsiblePerson> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreEuResponsiblePerson> buildQueryWrapper(ErpSellerStoreEuResponsiblePersonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreEuResponsiblePerson> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerId() != null, ErpSellerStoreEuResponsiblePerson::getSellerId, bo.getSellerId());
        lqw.eq(bo.getSellerStoreId() != null, ErpSellerStoreEuResponsiblePerson::getSellerStoreId, bo.getSellerStoreId());
        lqw.eq(bo.getCategoryId() != null, ErpSellerStoreEuResponsiblePerson::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getMsrEuId() != null, ErpSellerStoreEuResponsiblePerson::getMsrEuId, bo.getMsrEuId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpSellerStoreEuResponsiblePerson::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupport()), ErpSellerStoreEuResponsiblePerson::getSupport, bo.getSupport());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendA()), ErpSellerStoreEuResponsiblePerson::getExtendA, bo.getExtendA());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendB()), ErpSellerStoreEuResponsiblePerson::getExtendB, bo.getExtendB());
        lqw.eq(StringUtils.isNotBlank(bo.getExtendC()), ErpSellerStoreEuResponsiblePerson::getExtendC, bo.getExtendC());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreEuResponsiblePerson::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增店铺欧盟负责人
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreEuResponsiblePersonBo bo) {
        ErpSellerStoreEuResponsiblePerson add = BeanUtil.toBean(bo, ErpSellerStoreEuResponsiblePerson.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺欧盟负责人
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreEuResponsiblePersonBo bo) {
        ErpSellerStoreEuResponsiblePerson update = BeanUtil.toBean(bo, ErpSellerStoreEuResponsiblePerson.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreEuResponsiblePerson entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺欧盟负责人
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
